<template>
  <!-- // 分配角色 -->
  <div>
    这里将来会放置多选列表
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: center">
      <el-button type="primary" @click="doAssign">确定</el-button>
      <el-button @click="$emit('assignSuccess')">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getEmployee, assignRoles } from '@/api/employees'
export default {
  props: {
    id: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: []
    }
  },
  created() {
    this.loadRoles()
    this.loadRoleIds()
  },
  methods: {
    async loadRoles() {
      const res = await getRoles({ page: 1, pagesize: Infinity })
      this.roleList = res.data.data.rows
    },
    async loadRoleIds() {
      const res = await getEmployee(this.id)
      this.roleIds = res.data.data.roleIds
    },
    async doAssign() {
      await assignRoles({ id: this.id, roleIds: this.roleIds })
      this.$emit('assignSuccess')
    }
  }
}
</script>
